package com.zmj.framework.util;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.Cipher;
import java.io.ByteArrayOutputStream;
import java.security.Key;
import java.security.KeyFactory;
import java.security.spec.X509EncodedKeySpec;


public class DecryptUtils {
    protected static Logger logger = LoggerFactory.getLogger(DecryptUtils.class);

    public static final String KEY_ALGORITHM = "RSA";

    /** */
    /**
     * RSA最大加密明文大小
     */
    private static final int MAX_ENCRYPT_BLOCK = 245;

    /** */
    /**
     * RSA最大解密密文大小
     */
    private static final int MAX_DECRYPT_BLOCK = 256;

    public static byte[] decryptByPublicKey(byte[] encryptedData, String publicKey)
            throws Exception {
        byte[] keyBytes = Base64.decodeBase64(publicKey);
        X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        Key publicK = keyFactory.generatePublic(x509KeySpec);
        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(Cipher.DECRYPT_MODE, publicK);
        int inputLen = encryptedData.length;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int offSet = 0;
        byte[] cache;
        int i = 0;
        // 对数据分段解密
        while (inputLen - offSet > 0) {
            if (inputLen - offSet > MAX_DECRYPT_BLOCK) {
                cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK);
            } else {
                cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet);
            }
            out.write(cache, 0, cache.length);
            i++;
            offSet = i * MAX_DECRYPT_BLOCK;
        }
        byte[] decryptedData = out.toByteArray();
        out.close();
        return decryptedData;
    }

    public static void main(String[] args) {
        try {
            String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDunwL9yN6l7pfRjdMjp7LPSwyRXhCjFAU0KCPbshReum61J+o/29JQtIXdJCtL939LgvfURLmXxnfp7pXc/oyW6XjCw/wlgJTQGph4UV9WYFklGdpvMoPNsar7rKSDpsNVFzo9lxe518837ea7+pvlfx+0TN5NepTXqSJyesGeBwIDAQAB";

            String pStr = "VaawnKQMUX9oWdXYxW2e/lfIlZ8cNMelnCbY23gASx5yU0BSjl8nNI3WQbCTMQ0ePs8ZakAQyU+oAv2Bq8QY1SE2U+AliJB0/9MUiC1Dft/rzWlPXigqZWLim69VjIl0HLusoyKLn1ze0YTbTPPl/3qAQv2LI44dDivQEXaO9MWfpg8bc+//K6KbdCix/Utoq7PDVfwTNaklyz5HMThnjDew5ZkFsbsizp7MjXSGjKhhO50qBCqJrs8UpqlMu3XPYE1jpoTjwK0iJCmdoBj49slGGJ9lT7LSM1zV31aOX1UYaRIwY/jYwcAdPyWUjMW9jrcIJjYjxj6/MbiXl1rDMqAekJT764NDHpaBZ/obwqmQ3BndZnQj3AG8R6qMhzipPyXdf8C0kzbvHXXhq3Sgjs6NuyfoSSJOy/pTDwN7fdJWsGzCTvq3VkVq6ziBV0l6JXzH9RNyOTWGKkzfTi4bXgGReV8dhOAguuTfsIVslNCRmYRXnNtaaHDZO6O1tEDF";
            //decode
            String pwd_decoded = new String(decryptByPublicKey(Base64.decodeBase64(pStr), publicKey));
            System.out.println(pwd_decoded.trim());

        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }

    }
}